package com.wjk.kylin.config.web;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.wjk.kylin.config.properties.AliCloudOSSProperties;
import com.wjk.kylin.core.interceptor.CustomHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;


/**
 * @author wangjkui
 *         WEB配置类
 * @date 2020/5/22
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

//    @Value("${xss.enabled}")
//    private String enabled;
//
//    @Value("${xss.excludes}")
//    private String excludes;
//
//    @Value("${xss.urlPatterns}")
//    private String urlPatterns;

    @Autowired
    private CustomHandlerInterceptor customHandlerInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /** swagger配置 */
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * spring mvc拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(customHandlerInterceptor).addPathPatterns("/**");
    }

    /***
     * mybatisplus 分页插件
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    /***
     * 阿里云oss
     * @return
     */
    @Bean
    public OSS ossClient() {
        return new OSSClientBuilder().
                build(AliCloudOSSProperties.END_POIND, AliCloudOSSProperties.ACCESS_KEY_ID, AliCloudOSSProperties.ACCESS_KEY_SECRET);
    }

//    @Bean
//    public FilterRegistrationBean<XssFilter> xssFilterRegistration() {
//        FilterRegistrationBean<XssFilter> registration = new FilterRegistrationBean<>();
//        registration.setDispatcherTypes(DispatcherType.REQUEST);
//        registration.setFilter(new XssFilter());
//        registration.addUrlPatterns(StringUtils.split(urlPatterns, ","));
//        registration.setName("xssFilter");
//        registration.setOrder(Integer.MAX_VALUE);
//        Map<String, String> initParameters = MapUtil.newHashMap(2);
//        initParameters.put("excludes", excludes);
//        initParameters.put("enabled", enabled);
//        registration.setInitParameters(initParameters);
//        return registration;
//    }



}
